package cn.edu.mju.GlenDe.listeners;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Application Lifecycle Listener implementation class OnlineUserListener
 *
 */
@WebListener
public class OnlineUserListener implements HttpSessionAttributeListener, HttpSessionListener {

    /**
     * Default constructor. 
     */
    public OnlineUserListener() {
        // TODO Auto-generated constructor stub
    }
       
    /**
     * @see HttpSessionBindingEvent#HttpSessionBindingEvent(HttpSession, String)
     */
    public OnlineUserListener(HttpSession session, String name) {
        // TODO Auto-generated constructor stub
    }
       
    /**
     * @see HttpSessionBindingEvent#HttpSessionBindingEvent(HttpSession, String, Object)
     */
    public OnlineUserListener(HttpSession session, String name, Object value) {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
     */
    public void attributeRemoved(HttpSessionBindingEvent arg0)  { 
         // TODO Auto-generated method stub
    }

	/**
     * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
     */
    public void attributeAdded(HttpSessionBindingEvent arg0)  { 
         // TODO Auto-generated method stub
    	if("user".equals(arg0.getName()))
    	{
    		ServletContext application = arg0.getSession().getServletContext();
    		Object obj = application.getAttribute("online");
    		List<String> users = null;
    		if(obj != null)
    		{
    			users=(List<String> )obj;
    		}
    		else
    		{
    			users = new ArrayList<String>();
    		}
    			users.add(arg0.getValue().toString());
    			application.setAttribute("online", users);
    	}
    	System.out.println("Attribute Add:" + arg0.getName() + "=" + arg0.getValue());
    }

	/**
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent arg0)  { 
         // TODO Auto-generated method stub
    	System.out.println("Session Created");
    }

	/**
     * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
     */
    public void attributeReplaced(HttpSessionBindingEvent arg0)  { 
         // TODO Auto-generated method stub
    }

	/**
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent arg0)  { 
         // TODO Auto-generated method stub
    	HttpSession session = arg0.getSession();
    	Object obj = session.getAttribute("user"); 
    	if(obj != null)
    	{
    	   String sessionUserName = obj.toString();
    	   ServletContext application = arg0.getSession().getServletContext();
    	   Object obj1 = application.getAttribute("online"); 
    	   if(obj1 != null)
    	   {
    		   List<String> users = (List<String>)obj;
    		   Iterator it = users.iterator();
    		   while(it.hasNext())
    		   {
    		    	String uName = it.next().toString();
    			    if(uName.equals(sessionUserName))
    			   {
    			    	users.remove(uName);
    			    	application.setAttribute("online", users);
    			    	break;
    			   }
    		   }
    	   }
    	}
    	System.out.println("Session Destroyed");
    }
	
}
